
import { RetCodeDefine } from "../../../config/RetCodeDefine";
import { UserModel } from "../../../model/UserModel";
import DateUtil from "../../utils/DateUtil";
import { Logger } from "../../utils/Logger";
import { ProtoConfig } from "../protocol/ProtoConfig";
import { ProtoUtils } from "../protocol/ProtoUtils";
import { SocketConnection } from "./SocketConnection";
import { SocketConnectionManager } from "./SocketConnectionManager";
import { SocketMsg } from "./SocketMsg";
import { RetCodeModel } from "../../../model/RetCodeModel";
import { Md5Helper } from "../../utils/Md5Helper";

export class SocketClientMsgHandler {

    public static onEventSocketGata(socketConnect: SocketConnection, msg: SocketMsg) {
        // Logger.log("SocketClientMsgHandler onmsg=", socketConnect, msg);
        try {
            if (msg.protoName == ProtoConfig.req_register) {
                this.on_req_register(socketConnect, msg);
            }
            else if (msg.protoName == ProtoConfig.req_login) {
                this.on_req_login(socketConnect, msg);
            } else {
                if (socketConnect.isLogin()) {
                    if (msg.protoName == ProtoConfig.req_register) {

                    }
                    else {
                        Logger.error("不识别的Proto", msg.protoName)
                    }
                } else {
                    Logger.error("未登陆就发信息", msg.protoName)
                    this.sendErrorToClient(socketConnect, RetCodeDefine.LoginError_NoLogin)
                }
            }
        } catch (error) {
            Logger.error("SocketClientMsgHandler error==", error)
        }
    }

    //单纯把错误信息发送给客户端
    public static sendErrorToClient(conn: SocketConnection, retCode: number, reqId: number = 0) {
        let protoType: number = ProtoConfig.getProtoResponeseType(ProtoConfig.resp_error_code);
        let msg: string = RetCodeDefine.code2Msg.get(retCode);
        Logger.log("sendErrorToClient====", conn, retCode, msg);
        let model: RetCodeModel = RetCodeModel.createRetCodeModel(retCode, msg);
        let retCodeProto = RetCodeModel.serializeBinary(model);
        let socketMsg: SocketMsg = SocketMsg.createSocketMsg(reqId, protoType, ProtoConfig.resp_error_code, retCodeProto);
        conn.sendMsgToClient(socketMsg);
    }

    private static async on_req_register(socketConnect: SocketConnection, msg: SocketMsg) {
        let reqPack = ProtoUtils.DeserializeBinary(ProtoConfig.req_register, msg.protoData);
        let userName: string = reqPack.getUsername();
        let pass: string = reqPack.getPass();
        let ret: number = await UserModel.register(userName, pass);
        // Logger.log("on_req_register===", ret)
        let respPack = ProtoUtils.Create(ProtoConfig.resp_register);
        respPack.setRetcode(ret);
        socketConnect.createMsgToClient(msg.reqId, ProtoConfig.resp_register, respPack);
    }

    private static async on_req_login(socketConnect: SocketConnection, msg: SocketMsg) {
        let protoPack = ProtoUtils.DeserializeBinary(ProtoConfig.req_login, msg.protoData);
        let loginType:number = protoPack.getLogintype();
        let userName: string = protoPack.getUsername();
        let pass: string = protoPack.getPass();
        let sysInfoPack = protoPack.getSysinfo();
        let resultObj: object = await UserModel.login(userName, pass, sysInfoPack, loginType)
        let ret: number = resultObj["ret"];
        let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUser(userName, socketConnect);
        if (conn && conn.id != socketConnect.id) {
            this.sendErrorToClient(conn, RetCodeDefine.LoginError_MultiLogin);
            conn.gotoClose();
        }
        let respPack = ProtoUtils.Create(ProtoConfig.resp_login);
        respPack.setRetcode(ret);
        if (ret == RetCodeDefine.OK) {
            let user: UserModel = resultObj["user"];
            user.lastLoginIp = socketConnect.ip;
            user.lastLoginTime = DateUtil.now();
            user.token = Md5Helper.createToken(user.userName);
            user.toUsersRedis();
            socketConnect.user = user;
            respPack.setUserinfo(user.toProto());
        }
        Logger.log("onLogin===", protoPack, respPack)
        socketConnect.createMsgToClient(msg.reqId, ProtoConfig.resp_login, respPack);

    }

    public static sendZonInfoToAllClient() {
        // // let socketMsg: SocketMsg = ZoneModel.createZoneInfoSocketMsg();
        // for (let i = 0; i < SocketConnectionManager.instance.socketConnList.length; i++) {
        //     let conn: SocketConnection = SocketConnectionManager.instance.socketConnList[i];
        //     if (conn && conn.user && conn.user.roleType != UserModel.Role_Type.Server) {
        //         // conn.sendMsgToClient(socketMsg);
        //     }
        // }
    }

}

